package com.yidu.t299.controller.fxj;

import com.yidu.t299.domain.fxj.*;
import com.yidu.t299.service.fxj.SyllabussBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

@RestController
public class SyllabussController {
    @Autowired
    public SyllabussBiz syllabusBiz;

    /*
     * 实现下拉框数据,并返回 添加课程页面
     */
    @RequestMapping("syllabuss/comboBox")
    public ModelAndView comboBox(Model model) {
        // 查找教室
        List<ClassRoom> list = syllabusBiz.classroom();
        // 查找班级
        List<ClassS> classList = syllabusBiz.classSelect();
        // 查找老师
        List<Record> teacher = syllabusBiz.teacher();
        // 查找科目
        List<LessonDomain> lesson = syllabusBiz.lesson();
        ModelAndView mv = new ModelAndView();
        mv.setViewName("syllabusCRUD");// 跳转的页面
        mv.addObject("classroomList", list);// 教室
        mv.addObject("classList", classList);// 班级
        mv.addObject("teacherList", teacher);// 老师
        mv.addObject("lessonList", lesson);// 科目
        return mv;
    }

    // 新增到syllabus表
    @PostMapping("syllabuss/restful_syllabus")
    public String addSyllabus(@RequestBody Syllabus syllabus) {
        // syllabusId列的最大id值加1 当作下一个添加的主键值
        syllabus.setSyllabusId(syllabusBiz.syllabusIdMax() + 1);
        // 新增一行数据
        syllabusBiz.insert(syllabus);
        return "添加成功";
    }

    @GetMapping("syllabuss/restful_syllabus")
    // 查syllabus表
    public List<String[]> select() {
        // 拿到课程表数据
        List<SyllabusP> list = syllabusBiz.select();
        // 定义数组
        List<String[]> strings = new ArrayList<>();
        // 循环syllabus表数据
        for (int i = 0; i < list.size(); i++) {
            // 循环拿出里面的一行表数据
            SyllabusP syllabusP = list.get(i);
            // 将这一行数据按数组形式放入
            String[] syllabus = { syllabusP.getSyllabusDate(), syllabusP.getWeekName(), syllabusP.getAmpm(),
                    syllabusP.getRecordName(), syllabusP.getClassName(), syllabusP.getClassroomName(),
                    syllabusP.getLessonName() };
            // 存入数组集合
            strings.add(syllabus);
        }
        return strings;
    }

}
